package com.itheima.api;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.SmsHomeBrandMapper;
import com.itheima.pojo.SmsHomeAdvertise;
import com.itheima.pojo.SmsHomeBrand;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author lenovo
 * @description 针对表【sms_home_brand(首页推荐品牌表)】的数据库操作Service实现
 * @createDate 2022-10-11 14:26:18
 */
@DubboService
public class SmsHomeBrandApiImpl implements SmsHomeBrandApi {

    @Autowired
    private SmsHomeBrandMapper brandMapper;

    /**
     * 添加品牌
     *
     * @param brand
     */
    public void createBrand(SmsHomeBrand brand) {
        brandMapper.insert(brand);
    }

    /**
     * 分页查询品牌列表
     *
     * @param brandName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    public PageResult findBrands(String brandName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        //构造查询条件
        //分页构造器
        IPage<SmsHomeBrand> iPage = new Page<>(pageNum, pageSize);
        //条件构造器
        LambdaQueryWrapper<SmsHomeBrand> lqw = new LambdaQueryWrapper<>();
        //条件判断
        lqw.like(StrUtil.isNotBlank(brandName), SmsHomeBrand::getBrandName, brandName)
                .eq(recommendStatus != null, SmsHomeBrand::getRecommendStatus, recommendStatus)
                .orderByAsc(SmsHomeBrand::getSort);

        //System.out.println(advertise.getEndTime());
        brandMapper.selectPage(iPage, lqw);
        List<SmsHomeBrand> list = iPage.getRecords();
        Integer total = Convert.toInt(iPage.getTotal());//总条数
        Integer totalPage = Convert.toInt(iPage.getPages());//当前分页总页数
        return new PageResult(pageNum, pageSize, total, totalPage, list);
    }

    /**
     * 修改品牌排序
     *
     * @param brand
     */
    public void updateSortByid(SmsHomeBrand brand) {
        brandMapper.updateById(brand);
    }

    /**
     * 根据id查询品牌
     *
     * @param id
     * @return
     */
    public SmsHomeBrand findBrandById(Long id) {
        return brandMapper.selectById(id);
    }

    /**
     * 批量删除品牌
     *
     * @param ids
     * @return
     */
    public boolean deleteBrands(List<Long> ids) {
        //条件构造器
        LambdaQueryWrapper<SmsHomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.in(SmsHomeBrand::getId, ids);
        int delete = brandMapper.delete(lqw);
        return delete > 0;
    }

    /**
     * 批量修改品牌状态
     *
     * @param ids
     * @param recommendStatus
     * @return
     */
    public boolean updateRecommendStatus(List<Long> ids, Integer recommendStatus) {
        //条件构造器
        LambdaQueryWrapper<SmsHomeBrand> lqw = new LambdaQueryWrapper<>();
        lqw.in(SmsHomeBrand::getId, ids);
        SmsHomeBrand brand = new SmsHomeBrand();
        brand.setRecommendStatus(recommendStatus);
        int count = brandMapper.update(brand, lqw);
        return count > 0;
    }
}




